﻿using System;
using System.Web.Mvc;
using Netkk.Common.Models;

namespace Netkk.Library.ActionResults
{
    public class VCalendarResult : ActionResult
    {
        public Appointment Appointment { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            // verify properties 
            if (Appointment == null)
            {
                throw new ArgumentNullException("Appointment");
            }


            context.HttpContext.Response.Clear();
            context.HttpContext.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.ics", Appointment.Title));
            context.HttpContext.Response.ContentType = "text/calendar";

            VCalendar vCalendar = new VCalendar(new VCalendar.vEvent
            {
                HoleDay = false,
                UID = Appointment.Id.ToString(),
                DTStart = Appointment.Date,
                DTEnd = Appointment.Date.AddHours(3),
                DTStamp = DateTime.Now,
                Summary = Appointment.Title,
                Organizer = ".NET Stammtisch Konstanz-Kreuzlingen",
                Location = Appointment.Location,
                Description = Appointment.Description,
                URL = String.Format("http://www.dotnetkk.de/Appointments/Details/{0}", Appointment.Id),
                Classification = VCalendar.ClassificationType.PUBLIC,
                Priority = VCalendar.Priority.None
            });

            context.HttpContext.Response.Write(vCalendar.ToString());
        }

    }
}
